﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using YanhuaMMO;
using Spine;
using Spine.Unity;

public class BuffPlayMgr : MonoBehaviour {

    public GameObject leftRolePrefab;
    public GameObject rightMonsterPrefab;
    private GameObject playerObj;
    private GameObject monsterObj;
    private Coroutine playco;
    private GameObject attackcircle;

    // Use this for initialization
    void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}

    public void addNewRole()
    {
        if (leftRolePrefab)
        {
            if (playerObj)
                DestroyImmediate(playerObj);
            playerObj = Instantiate(leftRolePrefab);
            playerObj.transform.localPosition = new Vector3(298.15f, 300, 287.93f);
            playerObj.AddComponent<PerspectiveScale>();

            SkeletonAnimation anim = playerObj.transform.Find("Animation").GetComponent<SkeletonAnimation>();
            anim.state.SetAnimation(0, "idle01", true);

            attackcircle = Instantiate(Resources.Load<GameObject>("rangeattack"));
            attackcircle.transform.SetParent(playerObj.transform);
            attackcircle.transform.localPosition = new Vector3(0, 1, 0);
            attackcircle.transform.localScale = Vector3.one;
            attackcircle.transform.localEulerAngles = Vector3.zero;
        }
    }

    public void updateAttackRange(float radius)
    {
        if (attackcircle != null)
        {
            attackcircle.transform.localScale = playerObj.transform.localScale * radius;

        }
    }
    public void addRightNewRole()
    {
        if (rightMonsterPrefab)
        {
            if (monsterObj)
                DestroyImmediate(monsterObj);
            monsterObj = Instantiate(rightMonsterPrefab);
            monsterObj.transform.localPosition = new Vector3(301.83f, 300, 288.32f);
            monsterObj.AddComponent<PerspectiveScale>();
            monsterObj.transform.Find("Animation").localScale = new Vector3(-1, 1, 1);
        }
    }

    public bool IsRoleInit()
    {
        return playerObj && monsterObj;
    }

    public string[] getAllAnimation()
    {
        string[] clipname = new string[1];
        if (playerObj != null)
        {
            SkeletonAnimation anim = playerObj.transform.Find("Animation").GetComponent<SkeletonAnimation>();
            clipname = new string[anim.skeleton.Data.Animations.Items.Length + 1];
            clipname[0] = "null";
            for (int i = 0; i < anim.skeleton.Data.Animations.Items.Length; i++)
                clipname[i + 1] = anim.skeleton.Data.Animations.Items[i].Name;

        }
        else
            return null;
        return clipname;
    }

    private int getResourceIndex(string selected, string[] Res)
    {
        int index = -1;
        if (selected == "" || selected == "null")
            return 0;
        for (int i = 0; i < Res.Length; i++)
        {
            if (selected == Res[i])
            {
                index = i;
                break;
            }
        }
        return index;
    }

    public void addBuff(string buffid, string buffdata)
    {
        if (playco != null)
            StopCoroutine(playco);

        Util.CallMethod("CSharpModel", "addBuff", new object[] { buffid, buffdata, playerObj, monsterObj});
    }

}
